package com.allen.imsystem.user.service.impl;

import com.allen.imsystem.common.Const.GlobalConst;
import com.allen.imsystem.common.cache.CacheExecutor;
import com.allen.imsystem.common.cache.impl.UserCache;
import com.allen.imsystem.common.exception.BusinessException;
import com.allen.imsystem.common.exception.ExceptionType;
import com.allen.imsystem.file.service.FileService;
import com.allen.imsystem.user.mappers.UserInfoMapper;
import com.allen.imsystem.user.mappers.UserMapper;
import com.allen.imsystem.user.model.param.EditUserInfoParam;
import com.allen.imsystem.user.model.pojo.User;
import com.allen.imsystem.user.model.pojo.UserInfo;
import com.allen.imsystem.user.model.vo.UserInfoView;
import com.allen.imsystem.user.service.UserInfoService;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @ClassName UserInfoServiceImpl
 * @Description
 * @Version 1.0
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private FileService fileService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserCache userCache;

    @Override
    public UserInfoView findUserInfoView(String uid) {
        return CacheExecutor.get(userCache.userInfoViewCache, uid);
    }

    @Override
    public UserInfoView findUserInfoViewFromDB(String uid) {
        UserInfo userInfo = this.getById(uid);
        return new UserInfoView(userInfo);
    }


    @Override
    @Transactional
    public String uploadAvatar(MultipartFile multipartFile, String uid) {
        String avatarURL = fileService.uploadAvatar(multipartFile, uid);
        if (avatarURL != null) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUid(uid);
            userInfo.setAvatar(avatarURL);
            this.updateById(userInfo);
        } else {
            throw new BusinessException(ExceptionType.FILE_NOT_RECEIVE);
        }
        CacheExecutor.remove(userCache.userInfoViewCache, uid);
        return GlobalConst.Path.AVATAR_URL + avatarURL;
    }

    @Override
    public void updateUserInfo(EditUserInfoParam editUserInfoParam, String uid) {
        if (StringUtils.isEmpty(editUserInfoParam.getUsername()) || editUserInfoParam.getUsername().length() > 10) {
            throw new BusinessException(ExceptionType.PARAMETER_ILLEGAL, "用户名长度不应超过十个字符");
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(uid);
        userInfo.setUpdateTime(new Date());
        userInfo.setDesc(editUserInfoParam.getSignWord());
        userInfo.setAvatar(editUserInfoParam.getAvatar());
        userInfo.setGender(editUserInfoParam.getGender());
        userInfo.setUsername(editUserInfoParam.getUsername());
        this.updateById(userInfo);
        CacheExecutor.remove(userCache.userInfoViewCache, uid);
    }

    @Override
    public EditUserInfoParam getSelfInfo(String uid) {
        if (uid == null) {
            throw new BusinessException(ExceptionType.NO_LOGIN_ERROR);
        }
        User user = new LambdaQueryChainWrapper<>(userMapper)
                .eq(User::getUid, uid)
                .select(User::getEmail)
                .one();
        UserInfo userInfo = this.getById(uid);
        return new EditUserInfoParam(user.getEmail(), userInfo);
    }

}
